//
//  AppDelegate.m
//  UIPageViewController
//
//  Created by apple on 15/9/24.
//  Copyright © 2015年 戴维营. All rights reserved.
//

#import "AppDelegate.h"

#import "firstViewController.h"

#import "secondViewController.h"

#import "MMDrawerController.h"

#import "LeftViewController.h"

@interface AppDelegate ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate>

{
    NSArray *_viewArray;
    
    NSUInteger _currenIndex;
}

@end

@implementation AppDelegate


//与抽屉效果的协作

//1、将UIPageViewController对象作为MMDrawerController的centerController.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    [self.window makeKeyAndVisible];
    
    //创建分页控制器
    UIPageViewController *pageViewCtrl = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    
    //设置数据源代理
    pageViewCtrl.dataSource = self;
    pageViewCtrl.delegate = self;
    
    firstViewController * firstViewCtrl = [firstViewController new];
    secondViewController *secondViewCtrl = [secondViewController new];
    
    _viewArray = @[firstViewCtrl,secondViewCtrl];
    
    
    //设置可见页面
    
    [pageViewCtrl setViewControllers:@[firstViewCtrl] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    
    
    //设置抽屉效果
    
    LeftViewController *leftCtrl = [LeftViewController new];
    
    MMDrawerController *draweCtrl = [[MMDrawerController alloc] initWithCenterViewController:pageViewCtrl leftDrawerViewController:leftCtrl];
    
//    设置打开侧边栏的手势，默认不支持手势
    
    draweCtrl.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
    
//    设置关闭侧边栏效果，默认不支持
    
    draweCtrl.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
    
    self.window.rootViewController = draweCtrl;
    
    return YES;
}

#pragma mark - UIPageViewControllerDataSource

- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    if ([_viewArray indexOfObject:viewController])
    {
        return nil;
    }
    else
    {
        return _viewArray[1];
    }
}

//  viewController在这里表示的是当前可见的界面
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if ([_viewArray indexOfObject:viewController])
    {
        return _viewArray[0];
    }
    else
    {
        return nil;
    }
}

//完成翻页

#pragma mark - UIpageViewControllerDelegate

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
{
    _currenIndex = [_viewArray indexOfObject:pageViewController.viewControllers.firstObject];
    
    NSLog(@"Index: %ld",_currenIndex);
}

//设置翻页的效果
//翻页的位置（左中右）
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    //正面，从左往右翻，还是从右往左翻
    return UIPageViewControllerSpineLocationMin;
}


//如果为滚动翻页，实现下面的两个方法后，会显示UIPageControl指示页面

//总页数
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
    return 2;
}

//当前页号

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{

    return [_viewArray indexOfObject:pageViewController.viewControllers.firstObject];
}

//代理方法，允许多个手势同时识别

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
@end
